package cn.yunhe.servlet;

import cn.yunhe.entity.Product;
import cn.yunhe.service.ProductService;
import cn.yunhe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/GetProductByPriceServlet")
public class GetProductByPriceServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService productService=new ProductService();
        List<Product> products= productService.getMinPrice();
        String currPage = request.getParameter("pageIndex");
        if (currPage == null) {
            currPage = "1";
        }
        int pageIndex=Integer.parseInt(currPage);
        int totalDatas=productService.getProCount();
        int totalPages=PageUtil.getTotalPages(totalDatas);
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        if (pageIndex > totalPages) {
            pageIndex = totalPages;
        }
         HttpSession httpSession=request.getSession();
         httpSession.setAttribute("paroductList",products);
         httpSession.getAttribute("productList");
        request.setAttribute("pageIndex",pageIndex);

        for (Product product:products
             ) {

        }
        /*request.setAttribute("productList",products);*/
        request.getRequestDispatcher("product-list.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
